#pragma once
#include <iostream>
#include <string>
#include <cstring>

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

namespace inetaddr
{
    class InetAddr
    {
    public:
        InetAddr(std::string ip, uint16_t port)
            : _ip(ip), _port(port)
        {
            memset(&_psocket, 0, sizeof(_psocket));

            _psocket.sin_family = AF_INET;
            _psocket.sin_port = htons(port);
            inet_pton(AF_INET, _ip.c_str(), &_psocket.sin_addr);
        }

        InetAddr(sockaddr_in psocket)
        {
            _port = ntohs(psocket.sin_port);
            char ipstr[30];
            inet_ntop(AF_INET, &psocket.sin_addr, ipstr, sizeof(ipstr));
            _ip = ipstr;

            _psocket = psocket;
        }
        std::string getip()
        {
            return _ip;
        }

        uint16_t getport()
        {
            return _port;
        }

        sockaddr_in getsocket()
        {
            return _psocket;
        }

    private:
        sockaddr_in _psocket;
        std::string _ip;
        uint16_t _port;
    };
}